package net.ch2.pc12.tech.ackborn.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;

import net.ch2.pc12.tech.ackborn.dao.CacheDAO;
import net.ch2.pc12.tech.ackborn.dao.SourceDAO;

public class BbsModel {
	
	public static boolean hasCache(Context context){
		CacheDAO cdao = new CacheDAO(context);
		return cdao.hasBoardListCache();
	}

	public static BbsBoardModel getBoard(String id,Context context){
		CacheDAO cdao = new CacheDAO(context);
		return cdao.getBbsBoardModel();
	}
	
	public BbsModel(){
		
	}
		
	public void load(boolean forceReload,Context context){
		List<BbsBoardModel> list = null;
		CacheDAO cdao = new CacheDAO(context);
		
		if(forceReload){
			SourceDAO sdao = new SourceDAO(context);
			list = sdao.getBbsBoardModelList();
			cdao.storeBbsBoardModelList(list);
		}else{
			list = cdao.getBbsBoardModelList();
		}
		
		String grp = "";
		
		groupList = new ArrayList<HashMap<String,String>>();
		boardList = new ArrayList<List<HashMap<String,String>>>();

		List<HashMap<String,String>> tmpList = null;

		for(int i=0;i<list.size();i++){
			BbsBoardModel model = list.get(i);
			if(!grp.equals(model.getGroupName())){
				grp = model.getGroupName();
				HashMap<String,String> gmap = new HashMap<String,String>();
				gmap.put("groupName", grp);
				groupList.add(gmap);

				if(tmpList != null){
					boardList.add(tmpList);
				}
				tmpList = new ArrayList<HashMap<String,String>>();
			}
			HashMap<String,String> bmap = new HashMap<String,String>();
			bmap.put("boardName", model.getBoardName());
			bmap.put("boardId", model.getBoardId());
			bmap.put("boardUrl",model.getBoardUrl());
			tmpList.add(bmap);
		}
		boardList.add(tmpList);
		
	}
	
	private List<HashMap<String,String>> groupList;

	private List<List<HashMap<String,String>>> boardList;

	public int getGroupLength(){
		return groupList.size();
	}
	
	public List<HashMap<String,String>> getGroupNameList(){
		return groupList;
	}
	
	public List<List<HashMap<String,String>>> getGroupedChildNameList(){
		return boardList;
	}
	

}

